package com.jcfk.eam.service.base;

import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.eam.domain.dto.base.EamMakerDTO;
import com.jcfk.eam.domain.po.base.EamAssetBrand;
import com.jcfk.eam.domain.po.base.EamAssetType;
import com.jcfk.eam.domain.query.base.EamAssetBrandQuery;
import com.jcfk.eam.domain.dto.base.EamAssetBrandDTO;
import com.jcfk.eam.dao.base.EamAssetBrandDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.domain.uploadExcel.base.EamAssetBrandUploadExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 资产品牌
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-24
 */
@Service
public class EamAssetBrandService extends BaseService<EamAssetBrandDao,EamAssetBrand> {

    @Autowired
    private EamMakerService eamMakerService;

    /**
     * EamAssetBrand分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamAssetBrandDTO> queryByPage(DataPage<EamAssetBrandDTO> page, EamAssetBrandQuery query) {
        // 分页查询
        query.setBrandName(StringUtils.isEmpty(query.getBrandName()) ? query.getBrandName() : query.getBrandName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    public List<EamAssetBrandDTO> queryByPage(EamAssetBrandQuery query) {
        query.setBrandName(StringUtils.isEmpty(query.getBrandName()) ? query.getBrandName() : query.getBrandName() + "%");
        return this.baseMapper.queryByPage(query);
    }

    /**
     * 判断EamAssetBrand是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamAssetBrandDTO dto) {
        QueryWrapper<EamAssetBrand> qw = new QueryWrapper<>();
        Integer count = 0;
        // 判断编号
        if (!StrUtil.isEmpty(dto.getId())) {
            qw.lambda().ne(EamAssetBrand::getId, dto.getId());
        }
        qw.lambda().eq(EamAssetBrand::getBrandCode, dto.getBrandCode());
        qw.lambda().eq(EamAssetBrand::getDeleted, DeleteFlag.NOTDELETED.getValue());

        count = this.baseMapper.selectCount(qw);
        if (count > 0) {
            return RetResult.fail("编号已存在");
        }

        return RetResult.ok();
    }

    /**
     * 插入资产品牌
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamAssetBrandDTO dto) {
        // 保存
        EamAssetBrand item = new EamAssetBrand();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新资产品牌
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamAssetBrandDTO dto) {
        // 保存
        EamAssetBrand item = new EamAssetBrand();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除资产品牌(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamAssetBrandDTO dto) {

        EamAssetBrand item = new EamAssetBrand();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamAssetBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamAssetBrand item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取资产品牌信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamAssetBrandDTO get(String id) {
        // 查询数据
        EamAssetBrand item = this.baseMapper.selectById(id);
        EamAssetBrandDTO dto = new EamAssetBrandDTO();
        try {
            BeanUtils.copyProperties(item, dto);
            if(dto.getMakerId() == null || dto.getMakerId().isEmpty())
                return dto;
            EamMakerDTO maker = eamMakerService.get(dto.getMakerId());
            if(maker != null)
                dto.setMakerName(maker.getMakerName());
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入资产品牌
     *
     * @param list 批量插入资产品牌
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<EamAssetBrandUploadExcel> list) {

        List<EamAssetBrand> savelist = new ArrayList<>();
        list.forEach(itemOne -> {
            // 保存
            EamAssetBrand item = new EamAssetBrand();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamAssetBrandDTO>
     */
    public List<EamAssetBrandDTO> getList(EamAssetBrandQuery query) {
        // 查询条件
        QueryWrapper<EamAssetBrand> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getBrandCode()), EamAssetBrand::getBrandCode, query.getBrandCode());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getBrandName()), EamAssetBrand::getBrandName, query.getBrandName());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMakerId()), EamAssetBrand::getMakerId, query.getMakerId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getOrganizationId()), EamAssetBrand::getOrganizationId, query.getOrganizationId());
        qw.lambda().eq(query.getEnabled() != null, EamAssetBrand::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getDeleted() != null, EamAssetBrand::getDeleted, query.getDeleted());
        // 查询数据
        List<EamAssetBrand> list = super.list(qw);

        return convertList(list, EamAssetBrandDTO.class);
    }

    /**
     * 根据主键获取资产品牌信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamAssetBrand item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

